/*
 * WeekViewPanel.java
 *
 * Created on Oct 19, 2011, 12:57:46 PM
 */
package carassius.GUI.Calendar;

import carassius.BLL.Events.ArrayListWithEvents;
import carassius.BLL.GregorianCalendarWithEvents;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.GregorianCalendar;

/**
 *
 * @author siebz0r
 */
public class WeekViewPanel
		extends javax.swing.JPanel
		implements PropertyChangeListener
{
	//<editor-fold defaultstate="collapsed" desc="Fields">
	private GregorianCalendarWithEvents _calendar;
	private ArrayListWithEvents<Long> _markedDates;
	private int _weekOffset;
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Constructors">
	public WeekViewPanel()
	{
		this(new GregorianCalendarWithEvents(), new ArrayListWithEvents<Long>(), 0);
	}

	public WeekViewPanel(GregorianCalendarWithEvents calendar, ArrayListWithEvents<Long> markedDates, int weekOffset)
	{
		_calendar = calendar;
		_markedDates = markedDates;
		_weekOffset = weekOffset;
		initComponents();
		setLabels();
		_calendar.addPropertyChangeListener("year", this);
		_calendar.addPropertyChangeListener("month", this);
		_calendar.addPropertyChangeListener("firstDayOfWeek", this);
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Properties">
	public void setYear(int year)
	{
		_calendar.set(GregorianCalendar.YEAR, year);
	}

	public void setWeekOfYear(int week)
	{
		_calendar.set(GregorianCalendar.WEEK_OF_YEAR, week);
	}

	public int getWeekOfYear()
	{
		return _calendar.get(GregorianCalendar.WEEK_OF_YEAR);
	}

	public void setFirstDayOfWeek(int day)
	{
		_calendar.setFirstDayOfWeek(day);
	}

	public int getMonth()
	{
		return _calendar.get(GregorianCalendar.MONTH);
	}

	public void setDate(GregorianCalendar date)
	{
		_calendar.set(date.get(GregorianCalendar.YEAR), date.get(GregorianCalendar.MONTH), date.get(GregorianCalendar.DATE));
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Methods">
	private void setLabels()
	{
		GregorianCalendar cal = (GregorianCalendar) _calendar.clone();
		cal.set(GregorianCalendar.DATE, 1);
		cal.add(GregorianCalendar.WEEK_OF_YEAR, _weekOffset);

		cal.set(GregorianCalendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());

		uxDayButton1.setDate((GregorianCalendar)cal.clone());
		cal.add(GregorianCalendar.DAY_OF_MONTH, 1);

		uxDayButton2.setDate((GregorianCalendar)cal.clone());
		cal.add(GregorianCalendar.DAY_OF_MONTH, 1);

		uxDayButton3.setDate((GregorianCalendar)cal.clone());
		cal.add(GregorianCalendar.DAY_OF_MONTH, 1);

		uxDayButton4.setDate((GregorianCalendar)cal.clone());
		cal.add(GregorianCalendar.DAY_OF_MONTH, 1);

		uxDayButton5.setDate((GregorianCalendar)cal.clone());
		cal.add(GregorianCalendar.DAY_OF_MONTH, 1);

		uxDayButton6.setDate((GregorianCalendar)cal.clone());
		cal.add(GregorianCalendar.DAY_OF_MONTH, 1);

		uxDayButton7.setDate((GregorianCalendar)cal.clone());
	}
	//</editor-fold>

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        uxDayButton1 = new carassius.GUI.Calendar.DayButton(new GregorianCalendar(), _calendar, _markedDates);
        uxDayButton2 = new carassius.GUI.Calendar.DayButton(new GregorianCalendar(), _calendar, _markedDates);
        uxDayButton3 = new carassius.GUI.Calendar.DayButton(new GregorianCalendar(), _calendar, _markedDates);
        uxDayButton4 = new carassius.GUI.Calendar.DayButton(new GregorianCalendar(), _calendar, _markedDates);
        uxDayButton5 = new carassius.GUI.Calendar.DayButton(new GregorianCalendar(), _calendar, _markedDates);
        uxDayButton6 = new carassius.GUI.Calendar.DayButton(new GregorianCalendar(), _calendar, _markedDates);
        uxDayButton7 = new carassius.GUI.Calendar.DayButton(new GregorianCalendar(), _calendar, _markedDates);

        setMaximumSize(new java.awt.Dimension(189, 27));
        setName("Form"); // NOI18N
        setLayout(new java.awt.GridLayout(1, 7));

        uxDayButton1.setMaximumSize(new java.awt.Dimension(27, 27));
        uxDayButton1.setMinimumSize(new java.awt.Dimension(27, 27));
        uxDayButton1.setName("uxDayButton1"); // NOI18N
        uxDayButton1.setPreferredSize(new java.awt.Dimension(27, 27));
        uxDayButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DayButtonActionPerformed(evt);
            }
        });
        add(uxDayButton1);

        uxDayButton2.setMaximumSize(new java.awt.Dimension(27, 27));
        uxDayButton2.setMinimumSize(new java.awt.Dimension(27, 27));
        uxDayButton2.setName("uxDayButton2"); // NOI18N
        uxDayButton2.setPreferredSize(new java.awt.Dimension(27, 27));
        uxDayButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DayButtonActionPerformed(evt);
            }
        });
        add(uxDayButton2);

        uxDayButton3.setMaximumSize(new java.awt.Dimension(27, 27));
        uxDayButton3.setMinimumSize(new java.awt.Dimension(27, 27));
        uxDayButton3.setName("uxDayButton3"); // NOI18N
        uxDayButton3.setPreferredSize(new java.awt.Dimension(27, 27));
        uxDayButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DayButtonActionPerformed(evt);
            }
        });
        add(uxDayButton3);

        uxDayButton4.setMaximumSize(new java.awt.Dimension(27, 27));
        uxDayButton4.setMinimumSize(new java.awt.Dimension(27, 27));
        uxDayButton4.setName("uxDayButton4"); // NOI18N
        uxDayButton4.setPreferredSize(new java.awt.Dimension(27, 27));
        uxDayButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DayButtonActionPerformed(evt);
            }
        });
        add(uxDayButton4);

        uxDayButton5.setMaximumSize(new java.awt.Dimension(27, 27));
        uxDayButton5.setMinimumSize(new java.awt.Dimension(27, 27));
        uxDayButton5.setName("uxDayButton5"); // NOI18N
        uxDayButton5.setPreferredSize(new java.awt.Dimension(27, 27));
        uxDayButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DayButtonActionPerformed(evt);
            }
        });
        add(uxDayButton5);

        uxDayButton6.setMaximumSize(new java.awt.Dimension(27, 27));
        uxDayButton6.setMinimumSize(new java.awt.Dimension(27, 27));
        uxDayButton6.setName("uxDayButton6"); // NOI18N
        uxDayButton6.setPreferredSize(new java.awt.Dimension(27, 27));
        uxDayButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DayButtonActionPerformed(evt);
            }
        });
        add(uxDayButton6);

        uxDayButton7.setMaximumSize(new java.awt.Dimension(27, 27));
        uxDayButton7.setMinimumSize(new java.awt.Dimension(27, 27));
        uxDayButton7.setName("uxDayButton7"); // NOI18N
        uxDayButton7.setPreferredSize(new java.awt.Dimension(27, 27));
        uxDayButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DayButtonActionPerformed(evt);
            }
        });
        add(uxDayButton7);
    }// </editor-fold>//GEN-END:initComponents

	private void DayButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_DayButtonActionPerformed
	{//GEN-HEADEREND:event_DayButtonActionPerformed
		DayButton db = (DayButton) evt.getSource();
		if (!db.isSelected())
		{
			db.setSelected(true);
		}
		else
		{
			_calendar.set(db.getDate().get(GregorianCalendar.YEAR), db.getDate().get(GregorianCalendar.MONTH), db.getDate().get(GregorianCalendar.DATE));
		}
	}//GEN-LAST:event_DayButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private carassius.GUI.Calendar.DayButton uxDayButton1;
    private carassius.GUI.Calendar.DayButton uxDayButton2;
    private carassius.GUI.Calendar.DayButton uxDayButton3;
    private carassius.GUI.Calendar.DayButton uxDayButton4;
    private carassius.GUI.Calendar.DayButton uxDayButton5;
    private carassius.GUI.Calendar.DayButton uxDayButton6;
    private carassius.GUI.Calendar.DayButton uxDayButton7;
    // End of variables declaration//GEN-END:variables

	@Override
	public void propertyChange(PropertyChangeEvent evt)
	{
		setLabels();
	}
}
